package tmproject.hlhj.fhp.cartrans.activitys

import android.Manifest
import android.app.DatePickerDialog
import android.content.Intent
import android.view.View
import android.widget.DatePicker
import com.bumptech.glide.Glide
import com.huantansheng.easyphotos.EasyPhotos
import com.lzy.okgo.model.HttpParams
import com.tbruyelle.rxpermissions.RxPermissions
import kotlinx.android.synthetic.main.aty_zgcard.*
import rx.functions.Action1
import tmproject.hlhj.fhp.cartrans.BaseAty
import tmproject.hlhj.fhp.cartrans.IView.IdentifyView
import tmproject.hlhj.fhp.cartrans.IView.RegisView
import tmproject.hlhj.fhp.cartrans.IView.ZgView
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.customVIews.ChooseNumPop
import tmproject.hlhj.fhp.cartrans.customVIews.MyGlideEngin
import tmproject.hlhj.fhp.cartrans.http.Urls
import tmproject.hlhj.fhp.cartrans.javaBean.BaseBean
import tmproject.hlhj.fhp.cartrans.javaBean.CarNumBean
import tmproject.hlhj.fhp.cartrans.javaBean.RegisBean
import tmproject.hlhj.fhp.cartrans.presenter.IdentifyPresenter
import tmproject.hlhj.fhp.cartrans.presenter.RegisPresenter
import tmproject.hlhj.fhp.cartrans.presenter.ZgPresenter
import tmproject.hlhj.fhp.cartrans.utils.SpHelper
import tmproject.hlhj.fhp.cartrans.utils.ToastUtils
import java.io.File
import java.util.*

/**
 * Created by Administrator on 2018/8/28/028.
 */
class ZgAty :BaseAty<ZgView,ZgPresenter>(), ZgView, IdentifyView, RegisView {
    private var o1:String?=""
    private var ot1:String?=""
    private var mYear: Int=0
    private var cId:String?=null
    private var ctId:String?=null
    private var mDay: Int=0
    private var TakePhoto=130
    private lateinit var choosePop: ChooseNumPop

    private var mMonth: Int=0
    private lateinit var changePresenter: IdentifyPresenter
    private lateinit var regisPresenter: RegisPresenter
    override fun bindView(): ZgView {
        return this
    }

    override fun createPresenter(): ZgPresenter {
        changePresenter= IdentifyPresenter()
        changePresenter.createModule()
        changePresenter.bindView(this)

        regisPresenter= RegisPresenter()
        regisPresenter.createModule()
        regisPresenter.bindView(this)
        return ZgPresenter()
    }

    override fun getContentId(): Int {
        return R.layout.aty_zgcard
    }
    override fun initView() {

        choosePop=ChooseNumPop(this,0)

        val ca = Calendar.getInstance()
        mYear = ca.get(Calendar.YEAR)
        mMonth = ca.get(Calendar.MONTH)
        mDay = ca.get(Calendar.DAY_OF_MONTH)

        val infoBean = SpHelper.getDriver(this)
        val bean = infoBean?.data
        tvTimeLimit.text=bean?.zgz_expire_date
        o1=bean?.zgz_pic
        ot1=bean?.zgz_pic
        if (bean?.zgz_pic?.contains("uploads")!!){
            Glide.with(this).load(Urls.ImgBase+bean?.zgz_pic).into(img1)
        }else{
            Glide.with(this).load(bean?.zgz_pic).into(img1)
        }
        cId=bean?.vehicle_id
        ctId=bean?.vehicle_id
        tvNum.text=bean?.tractor_no
        if (!RxPermissions.getInstance(this).isGranted(Manifest.permission.CAMERA)||!RxPermissions.getInstance(this).isGranted(Manifest.permission.READ_EXTERNAL_STORAGE)){
            RxPermissions.getInstance(this).request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                    .subscribe { t ->
                        if (!t!!){
                            finish()
                        }
                    }
        }
    }
    override fun logicStart() {
        regisPresenter.getCarNum(this)
    }

    //无用
    override fun setIdentify() {
    }

    override fun changeInfo(base: BaseBean) {
        ToastUtils.toast("修改成功")
        val bean = SpHelper.getDriver(this)
        val data = bean?.data
        data?.tractor_no=tvNum.text.toString()
        data?.zgz_pic=ot1
        data?.zgz_expire_date=tvTimeLimit.text.toString()
        SpHelper.saveDriver(this,bean!!)
        finish()
    }
    //无用
    override fun regisResp(regisBean: RegisBean) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun carNumResp(carNumBean: CarNumBean) {
        choosePop.setData(carNumBean.data as ArrayList<CarNumBean.DataBean>)
    }

    override fun setListener() {
        btSave.setOnClickListener {
            val httpParams = HttpParams()
            httpParams.put("type","ZGZ")
            httpParams.put("token",SpHelper.getToken(this))
            if (null==ctId){
                if (null!=cId){
                    httpParams.put("vehicle_id",cId)
                }
            }
            httpParams.put("zgz_expire_date",tvTimeLimit.text.toString())
            if (!o1?.equals(ot1)!!){
             httpParams.put("zgz_pic",File(ot1))
            }
            changePresenter.saveInfo(this,httpParams)
        }

        choosePop.listener=object :ChooseNumPop.OnNumSelectListener{
            override fun selectNum(id: Int, num: String) {
                cId=id.toString()
                tvNum.text=num
            }
        }
        btExit.setOnClickListener {
            finish()
        }

        tvTimeLimit.setOnClickListener {
            DatePickerDialog(this, object : DatePickerDialog.OnDateSetListener{
                override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                    val days: String
                    if (month + 1 < 10) {
                        if (dayOfMonth < 10) {
                            days = StringBuffer().append(year).append("-").append("0").append(month + 1).append("-").append("0").append(dayOfMonth).append("").toString()
                        } else {
                            days = StringBuffer().append(year).append("-").append("0").append(month + 1).append("-").append(dayOfMonth).append("").toString()
                        }

                    } else {
                        if (dayOfMonth < 10) {
                            days = StringBuffer().append(year).append("-").append(month + 1).append("-").append("0").append(dayOfMonth).append("").toString()
                        } else {
                            days = StringBuffer().append(year).append("-").append(month + 1).append("-").append(dayOfMonth).append("").toString()
                        }

                    }
                    tvTimeLimit.text = days

                }
            }, mYear, mMonth, mDay).show()
        }

        tvNum.setOnClickListener {
            if (!choosePop.isShowing){
                ToastUtils.toast("弹出")
                choosePop.showAsDropDown(tvNum)
            }else{
                ToastUtils.toast("几个意思")
                choosePop.dismiss()
            }
        }

        btEdit.setOnClickListener {
            btSave.visibility=View.VISIBLE
            tvTimeLimit.isClickable=true
            tvNum.isClickable=true
            img1.isClickable=true
        }
        img1.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("tmproject.hlhj.fhp.fileprovider")//参数说明：见下方`FileProvider的配置`
                    .start(TakePhoto)
        }
        tvTimeLimit.isClickable=false
        tvNum.isClickable=false
        img1.isClickable=false
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
        when(requestCode){
            TakePhoto->{
                ot1=pathList[0]
                Glide.with(this).load(ot1).into(img1)
            }
        }
    }
}